package edu.qd.learn.newchapter.chapter_thread.day02;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 布衣
 */
public class ThirdPool extends Thread {

    public static volatile int ticket = 1;

    @Override
    public void run() {
        //同步代码块
        if (ticket < 101) {
            System.out.println(getName() + "正在出售第" + ticket++ + "张票");
        }
    }

    public ThirdPool() {

    }

    public ThirdPool(String name) {
        super(name);
    }

    public static void main(String[] args) {
        ThirdPool t1 = new ThirdPool("窗口1");
        ThirdPool t2 = new ThirdPool("窗口2");
        ThirdPool t3 = new ThirdPool("窗口3");
        ExecutorService es = Executors.newSingleThreadExecutor();

        for (int i = 0;ThirdPool.ticket < 101; i++) {
            es.execute(t1);
            es.execute(t2);
            es.execute(t3);
        }

        es.shutdown();
    }

}